﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;

namespace Vmago.Models
{
    public class RepositoyUserProfile:IUserProfile
    {
        #region IUserProfile Members

        private UserProfileDataContext dataContext;

        public RepositoyUserProfile()
        {
            dataContext = new UserProfileDataContext();
        }

        public List<UserProfile> GetUserProfile()
        {
            return dataContext.UserProfiles.ToList();
        }

        public IEnumerable<UserProfile> GetSingleUserProfile()
        {

            IEnumerable<UserProfile> UserProfile = from p in dataContext.UserProfiles
                                                   where p.UserId.Equals(Membership.GetUser().ProviderUserKey)
                                                   select p;
                             
            
            return  UserProfile;
         
        }

        public void UpdateUserProfile( string username,string Email, string Name, int Gender, int Year, int Month, int Day, int Occupation,int Marital, int Body, int Blood, string Hometown
                                       , string Postcode, string CurrentAddress, string Landline, string Mobile, string QQ, string Msn)
        {

            var u = (from p in dataContext.UserProfiles
                     where p.UserId.Equals(username)
                     select p).Single();
            //Membership.GetUser().ProviderUserKey
            var k = (from p in dataContext.UserMemberships
                     where p.UserId.Equals(username)
                     select p).Single();

            if (Email != null)
                k.Email = Email;
            else
                k.Email = null;

            if (Name != null) 
                 u.Name = Name; 
            else 
                 u.Name =null;

           
            u.Gender = Gender;
            u.Birthday = DateTime.Parse(Year.ToString() + "-" + Month.ToString() + "-" + Day.ToString());
            u.Marital = Marital;
            u.Body = Body;
            u.Blood =Blood;
            u.Occupation = Occupation;

            if (Hometown != null ) 
                u.Hometown = Hometown;
            else 
                u.Hometown =null;

            if (Postcode!=null)    
                u.PostCode= Postcode;
            else 
                u.PostCode = null;

            if (CurrentAddress != null)
                u.CurrentAddreass =CurrentAddress ;
            else 
                u.CurrentAddreass=null;

            if (Landline != null )
                u.Landline = Landline;
            else 
                u.Landline =null;
            
            if (Mobile != null ) 
                u.Mobile = Mobile;
            else
                u.Mobile =null;

            if (QQ != null) 
                u.QQ =QQ;
            else 
                u.QQ = null;

            if (Msn !=null)
                u.MSN =Msn;
            else 
                u.MSN = null;


            dataContext.SubmitChanges();
        }

        //验证用户名唯一性
        public IEnumerable<User> ValidateUserName(string username)
        {
            IEnumerable<User> c = from p in dataContext.Users
                                                      where p.UserName.Equals(username)
                                                      select p;
            return c;
        }

        //验证用户邮箱唯一性
        public IEnumerable<UserMembership> ValidateUserMail(string mail)
        {
            IEnumerable<UserMembership> c = from p in dataContext.UserMemberships
                                          where p.Email.Equals(mail)
                                         select p;
            return c;
        }
        #endregion
    }
}
